<?php

namespace addons\crontab\model;

use think\Model;
use think\Db;
use addons\crontab\model\Rdrecords01extra;

//采购入库明细
class Rdrecords01 extends Model
{
    protected $connection = "database.inc";
    protected $table = "rdrecords01";
    
    /*
     * 新增数据（表体）,返回表头ID
     *   */
    public function insertDetail($data,$u8type){
        if(!$data){
            exception('rdrecords01表体为空');
        }
        
        //表头自增ID
        if(!isset($data['ID'])){
            exception('rdrecords01表头ID为空');
        }
        
        $model =  self::where([
            'ID' => $data['ID'],
            'cInvCode' => $data['number'],
            'irowno' => $data['row_id']
        ])
        ->find();
        
        if(!$model){
            //表体
            $model = self::create([
                'AutoID' => Crontab::getAutoIDByTname('rdrecords01'),//iNatUnitPrice
                'ID' => $data['ID'],//表头id
                'cInvCode'=>$data['number'],
                'iQuantity' => $data['quantity'],
                'iPOsID' => isset($data['auto_id'])?$data['auto_id']:'',//采购订单子表标识ID
                'iNQuantity' => $data['quantity'],
                'chVencode' => isset($data['chVencode'])?$data['chVencode']:'',//表头供应商code
                'cPOID' => isset($data['source_code'])?$data['source_code']:'',//po2023070206
                'irowno' => $data['row_id'], //行号
                'iUnitCost' => isset($data['unit_price'])?$data['unit_price']:'',//单价 
                'iPrice' => isset($data['unit_price'])?round($data['unit_price']*$data['quantity'],2):'',//金额 
                'iAPrice' => isset($data['unit_price'])?round($data['unit_price']*$data['quantity'],2):'',//暂估金额 
                'iSQuantity' => 0,
                'iSNum' => 0,
                'iMoney' => 0,
                'fACost' => isset($data['unit_price'])?$data['unit_price']:'',//暂估单价 
                'iOriTaxCost' => isset($data['unit_price'])?$data['unit_price']*1.13:'',//原币含税单价 
                'iOriCost' => isset($data['unit_price'])?$data['unit_price']:'',//原币无税单价 
                'iOriMoney' => isset($data['unit_price'])?round($data['unit_price']*$data['quantity'],2):'',//原币无税金额 
                'iOriTaxPrice' => isset($data['unit_price'])?round($data['unit_price']*$data['quantity']*0.13,2):'',//原币税额 
                'ioriSum' => isset($data['unit_price'])?round($data['unit_price']*$data['quantity']*1.13,2):'',//原币价税合计 
                'iTaxRate' => 13,
                'iTaxPrice' => isset($data['unit_price'])?round($data['unit_price']*$data['quantity']*0.13,2):'',//本币税额 
                'iSum' => isset($data['unit_price'])?round($data['unit_price']*$data['quantity']*1.13,2):'',//本币价税合计 
                'bTaxCost' => 1,
                'iExpiratDateCalcu' => 0,
                'iordertype' => 0,
                'isotype' => 0,
                'bCosting' => 1 //单据是否核算　 是
            ]);
            
            if($u8type==1&&isset($data['auto_id'])){
                //采购订单表体扩展表 协议部分  采购入库单明细
                $podetails_extra = Db::connect('database.sec')->table('PO_Podetails_extradefine')
                ->where('ID',$data['auto_id'])
                ->field('cbdefine1,cbdefine2,cbdefine3,cbdefine4,cbdefine5')
                ->find();
                if($podetails_extra&&isset($podetails_extra['cbdefine1'])){
                    $podetails_extra['AutoID'] = $model->AutoID;
                    unset($podetails_extra['ROW_NUMBER']);
                    Rdrecords01extra::create($podetails_extra);
                }
            }
        }
        //更新pda单据表，中间库表体id
        db('temporary')->where('id',$data['temporary_id'])->update([
            'mid_rdrecord_id' => $data['ID'],
            'mid_rdrecords_id' => $model->AutoID,
            'unit_price' => isset($data['unit_price'])?$data['unit_price']:'',
            'total_price' => isset($data['unit_price'])?$data['unit_price']*$data['quantity']:''
        ]);
    }
}
